﻿using System;
using Assets.Scripts.GtsScripts;
using UnityEngine;

// Token: 0x020002C7 RID: 711
public class GTSMovement : MonoBehaviour
{
	// Token: 0x0600108C RID: 4236 RVA: 0x00079779 File Offset: 0x00077979
	public void SetGiantess(Giantess giantess)
	{
		this.giantess = giantess;
		this.giantess.rbody = this.rbody;
		this.rbody.constraints = RigidbodyConstraints.FreezeRotation;
		base.transform.position = giantess.myTransform.position;
	}

	// Token: 0x0600108D RID: 4237 RVA: 0x000797B8 File Offset: 0x000779B8
	private void Awake()
	{
		this.terrainCollider = UnityEngine.Object.FindObjectOfType<TerrainCollider>();
		this.rbody = base.gameObject.AddComponent<Rigidbody>();
		this.rbody.freezeRotation = true;
		this.gravity = base.gameObject.AddComponent<Gravity>();
		this.gravity.baseScale = 100f;
		this.capsuleCollider = base.gameObject.AddComponent<CapsuleCollider>();
		this.capsuleCollider.center = new Vector3(0f, 800f, 0f);
		this.capsuleCollider.radius = 200f;
		this.capsuleCollider.height = 1600f;
		base.gameObject.layer = Layers.gtsCapsuleLayer;
	}

	// Token: 0x0600108E RID: 4238 RVA: 0x00079870 File Offset: 0x00077A70
	private void Update()
	{
		if (base.transform.lossyScale.y != this.giantess.Scale)
		{
			base.transform.localScale = Vector3.one * this.giantess.Scale;
		}
		this.UpdateCharacterPosition();
		this.CollisionChoose();
	}

	// Token: 0x0600108F RID: 4239 RVA: 0x000798CC File Offset: 0x00077ACC
	private void CollisionChoose()
	{
		if (this.terrainCollider && this.capsuleCollider)
		{
			float y = base.transform.lossyScale.y;
			if (this.terrainColliderActive && y > this.terrainScale)
			{
				Physics.IgnoreCollision(this.capsuleCollider, this.terrainCollider, true);
				this.terrainColliderActive = false;
				this.DisableGrounder(true);
				Debug.Log("Collision OFF");
			}
			else if (!this.terrainColliderActive && y < this.terrainScale)
			{
				Physics.IgnoreCollision(this.capsuleCollider, this.terrainCollider, false);
				this.terrainColliderActive = true;
				this.DisableGrounder(false);
				Debug.Log("Collision ON");
			}
		}
	}

	// Token: 0x06001090 RID: 4240 RVA: 0x00079994 File Offset: 0x00077B94
	private void DisableGrounder(bool disable)
	{
		if (this.ik == null)
		{
			this.ik = this.giantess.GetComponent<GiantessIK>();
		}
	}

	// Token: 0x06001091 RID: 4241 RVA: 0x000799B8 File Offset: 0x00077BB8
	private void UpdateCharacterPosition()
	{
		float deltaTime = Time.deltaTime;
		Transform transform = base.transform;
		if (this.onlyMoveWithPhysics || this.giantess.movement.move)
		{
			this.MoveTransformToCapsule();
			this.giantess.myTransform.rotation = Quaternion.Slerp(this.giantess.myTransform.rotation, transform.rotation, 10f * deltaTime);
		}
		else
		{
			this.MoveCapsuleToTransform();
			transform.rotation = this.giantess.myTransform.rotation;
		}
	}

	// Token: 0x06001092 RID: 4242 RVA: 0x00079A4C File Offset: 0x00077C4C
	private void MoveCapsuleToTransform()
	{
		Vector3 position = this.giantess.myTransform.position;
		this.rbody.position = position;
	}

	// Token: 0x06001093 RID: 4243 RVA: 0x00079A78 File Offset: 0x00077C78
	private void MoveTransformToCapsule()
	{
		if (this.doNotMoveGts)
		{
			return;
		}
		float deltaTime = Time.deltaTime;
		Vector3 a = CenterOrigin.VirtualToWorld(this.giantess.virtualPosition);
		Vector3 world_pos = Vector3.Lerp(a, base.transform.position, deltaTime * 10f);
		this.giantess.Move(world_pos);
	}

	// Token: 0x06001094 RID: 4244 RVA: 0x00079ACD File Offset: 0x00077CCD
	public void EnableCollider(bool enable)
	{
		this.capsuleCollider.enabled = enable;
		this.gravity.enabled = enable;
	}

	// Token: 0x04001554 RID: 5460
	private LayerMask gtsLayer;

	// Token: 0x04001555 RID: 5461
	public Giantess giantess;

	// Token: 0x04001556 RID: 5462
	private CapsuleCollider capsuleCollider;

	// Token: 0x04001557 RID: 5463
	private Rigidbody rbody;

	// Token: 0x04001558 RID: 5464
	private bool terrainColliderActive = true;

	// Token: 0x04001559 RID: 5465
	private TerrainCollider terrainCollider;

	// Token: 0x0400155A RID: 5466
	private float terrainScale = 20f;

	// Token: 0x0400155B RID: 5467
	private GiantessIK ik;

	// Token: 0x0400155C RID: 5468
	public bool onlyMoveWithPhysics;

	// Token: 0x0400155D RID: 5469
	private Gravity gravity;

	// Token: 0x0400155E RID: 5470
	public bool doNotMoveGts;
}
